using MediatR;
using RBAC.ErrorCode;
using System.ComponentModel.DataAnnotations;

namespace RBAC.Read.Api.Application.Command.ERP.SalesManagement.Return
{
    /// <summary>
    /// 获取退货通知单明细列表查询命令
    /// </summary>
    public class GetReturnNoticeDetailListCommand : IRequest<APIResult<GetReturnNoticeDetailListDto>>
    {
        /// <summary>
        /// 页码
        /// </summary>
        [Range(1, int.MaxValue)]
        public int PageIndex { get; set; } = 1;

        /// <summary>
        /// 每页条数
        /// </summary>
        [Range(1, 1000)]
        public int PageSize { get; set; } = 10;

        /// <summary>
        /// 单据编码
        /// </summary>
        public string? BillCode { get; set; }

        /// <summary>
        /// 客户名称
        /// </summary>
        public string? CustomerName { get; set; }

        /// <summary>
        /// 物料名称
        /// </summary>
        public string? MaterialName { get; set; }

        /// <summary>
        /// 物料编码
        /// </summary>
        public string? MaterialCode { get; set; }

        /// <summary>
        /// 销售订单号
        /// </summary>
        public string? SalesOrderNumber { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime? StartDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime? EndDate { get; set; }
    }

    /// <summary>
    /// 退货通知单明细DTO
    /// </summary>
    public class ReturnNoticeDetailDto
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        public long Id { get; set; }

        /// <summary>
        /// 单据编码
        /// </summary>
        public string? BillCode { get; set; }

        /// <summary>
        /// 客户名称
        /// </summary>
        public string? CustomerName { get; set; }

        /// <summary>
        /// 单据日期
        /// </summary>
        public DateTime? BillDate { get; set; }

        /// <summary>
        /// 单据状态
        /// </summary>
        public string? BillStatus { get; set; }

        /// <summary>
        /// 业务状态
        /// </summary>
        public string? BusinessStatus { get; set; }

        /// <summary>
        /// 销售订单号
        /// </summary>
        public string? SalesOrderNumber { get; set; }

        /// <summary>
        /// 物料编码
        /// </summary>
        public string? MaterialCode { get; set; }

        /// <summary>
        /// 物料名称
        /// </summary>
        public string? MaterialName { get; set; }

        /// <summary>
        /// 型号规格
        /// </summary>
        public string? Specification { get; set; }

        /// <summary>
        /// 主计量
        /// </summary>
        public string? Unit { get; set; }

        /// <summary>
        /// 退货数量
        /// </summary>
        public int? ReturnQuantity { get; set; }

        /// <summary>
        /// 入库数量
        /// </summary>
        public int? StockInQuantity { get; set; }

        /// <summary>
        /// 补发数量（计算字段：退货数量-入库数量）
        /// </summary>
        public int? ReissueQuantity => ReturnQuantity.HasValue && StockInQuantity.HasValue 
            ? ReturnQuantity.Value - StockInQuantity.Value 
            : ReturnQuantity;

        /// <summary>
        /// 批号
        /// </summary>
        public string? BatchNumber { get; set; }

        /// <summary>
        /// 退货原因
        /// </summary>
        public string? ReturnReason { get; set; }

        /// <summary>
        /// 处理方式
        /// </summary>
        public string? ProcessingMethod { get; set; }

        /// <summary>
        /// 仓库
        /// </summary>
        public string? Warehouse { get; set; }

        /// <summary>
        /// 操作员
        /// </summary>
        public string? Operator { get; set; }

        /// <summary>
        /// 审批员
        /// </summary>
        public string? Reviewer { get; set; }

        /// <summary>
        /// 审批日期
        /// </summary>
        public DateTime? ReviewDate { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
    }

    /// <summary>
    /// 退货通知单明细列表返回DTO
    /// </summary>
    public class GetReturnNoticeDetailListDto
    {
        /// <summary>
        /// 明细数据列表
        /// </summary>
        public List<ReturnNoticeDetailDto> PageDatas { get; set; } = new List<ReturnNoticeDetailDto>();

        /// <summary>
        /// 总记录数
        /// </summary>
        public int TotalCount { get; set; }
    }
}